<?php
InitGP(array('page','action')); //初始化变量全局返回
include_once(INC_PATH."/delivery.class.php");
include_once(INC_PATH."/goodsforecast.class.php");
$d = new DeliveryClass();
$g = new GoodsForecastClass();
$uid = $_USERS['uid'];
$uname = $_USERS['uname'];
if($action == 'pull'){
	//进行收货操作
	InitGP(array('did'));
	$data = array();
	$data['status'] = 8;
	if($d->edit($did, $data)){
		showmsg('收货成功', '?name=goodsdelivery');
	}else{
		showmsg('收货失败', -1);
	}
}else if($action == 'pay'){
	InitGP(array('did', 'status', 'cid'));
	$delivery = $d->getone($did);
	$allPrice = $delivery['price'] +  $delivery['box_price'] +  $delivery['firm_price'];
	//获取优惠券 0未激活 1已经激活 2出售中 3已使用
	include_once(INC_PATH."/coupon.class.php");
	$coupon=CouponClass::init();
	$uid=$_USERS['uid'];

	if($status == 5){
		include_once(INC_PATH."/member.class.php");
		$m = new memberclass();
		//判断钱是否够支付
		$money = $m->getmoneybyuname($_USERS['uname']);
		$cid = intval($cid);
		if($cid){
			$cd = $coupon->getone($cid);
			if($cd){
				if($cd['uid'] == $uid){
					$allPrice =  $allPrice - $cd['money'];
					if($allPrice < 0){
						$allPrice = 0;
					}
				}else{
					showmsg('你不能用不属于你的优惠券', -1);
				}
			}else{
				showmsg('你选择的优惠券不存在', -1);
			}
		}
		if($money >= $allPrice){
			$note = '支付运费 '.$delivery['id'];
			$rs = $m->moneyedit( $_USERS['uname'], -1 * $allPrice, 2, $note);
			if($rs == 'OK'){
				$d->editStatus($did, $status);
				//修改优惠券
				if($cid){
					$dataarray['state'] = 3;
					$coupon->edit($cid, $dataarray);
				}
				showmsg($delivery['id'].' 支付成功 ', '?name=delivery');
			}
		}else{
			showmsg('你的余额不足，请尽快充值', -1);
		}
	}
	$wheresql =' uid = '.$uid.' AND state = 1';
	$couponarray = $coupon->getdata("",$wheresql,""); //获取数据
	include template('member_goods_delivery_pay');
}else if($action == 'detail'){
	InitGP(array('did', 'status'));
	if($status == 3){
		$rs = $d->editStatus($did, $status);
		if($rs != 'OK'){
			showmsg('审核订单失败！', -1);
		}
	}
	$delivery = $d->getone($did);
	$delivery['status_info'] = $d->getStatusInfo($delivery['status']);
	$goods = array();
	$allWeight = 0;
	if($delivery['gids']){
		$gids = explode(',', $delivery['gids']);
		foreach ($gids as $gid){
			if($gid){
				$good = $g->getone($gid);
				$goods[] = $good;
				$allWeight = $good['weight'] + $allWeight;
			}
		}
	}
	if(!$delivery){
		showmsg('运单信息不正确', -1);
	}
	$allPrice = $delivery['price'] +  $delivery['box_price'] +  $delivery['firm_price'];
	include template('member_goods_delivery_detail');
}else if($action == 'comment'){
	InitGP(array('did', 'status', 'comment'));
	$delivery = $d->getone($did);
	if(!$delivery){
		showmsg('运单信息不正确', -1);
	}
	if($status == 9){			//状态为 9 才是评论状态
		if($comment){
		$data = array();
		$data['status'] = 9;
		$data['comment'] = Char_cv($comment);
		$data['commenttime'] = time();
		$d->edit($did, $data);
		showmsg('评论成功', '?name=goodsdelivery&action=detail&did='.$did);
	}else{
		showmsg('评论不能为空', -1);
	}
	}
	include template('member_goods_delivery_comment');
}else{
	$Table=new TableClass("goods_delivery","did");
	AjaxHead();//禁止页面缓存
	$total = $Table->getcount();
	$pagesize = 15;                                                                                           //一页显示信息数
	$page = isset($page) ? max(1, intval($page)) : 1;             //处理页码变量
	$offset = ($page-1) * $pagesize;        //偏移量
	$orderstr = 'did DESC';
	$wheresql = 'uid = '.$uid.' AND status >= 8';
	$dataarray = $Table->getdata("$offset,$pagesize", $wheresql, $orderstr);
	foreach ($dataarray as $key=>$ex){
		$dataarray[$key]['status_info'] = $d->getStatusInfo($ex['status']);
	}
	include template('member_goods_delivery_list');
}
?>